package com.mustafaiev.tair.cts.tapestry.pages.baskets;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.SelectModelFactory;

import com.mustafaiev.tair.cts.facade.IPayerFacade;
import com.mustafaiev.tair.cts.factory.ICostTrackingSystemFacadeFactory;
import com.mustafaiev.tair.cts.tapestry.dto.BasketDTO;
import com.mustafaiev.tair.cts.tapestry.select.PayerSelectModel;

public class Create extends ManageBasket {

	@Inject
	private ICostTrackingSystemFacadeFactory facadeFactory;

	@InjectPage
	private Index basketsPage;

	@InjectPage
	private AddPurchases addPurchasesPage;

	@Property
	private SelectModel model;

	@Inject
	private SelectModelFactory selectModelFactory;

	@Property
	@Persist
	private BasketDTO basket;

	@Component(id = "createBasketForm")
	private Form form;

	void onPrepareForRender() {
		this.model = new PayerSelectModel(
				this.facadeFactory.getFacade(IPayerFacade.class));

		if (this.basket == null) {
			this.basket = new BasketDTO();
		}
	}

	Object onSuccessFromCreateBasketForm() {
		this.addPurchasesPage.init(this.basket);
		return this.addPurchasesPage;
	}

	@OnEvent(value = "back")
	Object goBack() {
		return this.basketsPage;
	}

	public void init(final BasketDTO dto) {
		this.basket = dto;
	}

}
